package com.utils.picasso;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.widget.ImageView;

import com.gxpt.app.Constant;
import com.gxpt.app.R;
import com.squareup.picasso.Picasso;

import java.io.File;

/**
 * Created by dufeng on 2017/4/20.
 */

public class PicassoUtils {


    public static void Picasso_Circle(Context context,int ids,int size,ImageView imageView){
        Picasso.with(context).load(ids).resize(size, size).transform(new CircleTransform()).into(imageView);
    }

    public static void Picasso_Circle(Context context,String Url,int size,ImageView imageView){
        Picasso.with(context).load(Url).resize(size, size).transform(new CircleTransform()).into(imageView);
    }

    /**
     * 网络图片显示
     */
    public static void Picasso_displayImage(Context context, String Url,int width,int height,ImageView imageView) {

        Picasso.with(context)
                .load(Url)
                .resize(width,height)
                .config(Bitmap.Config.RGB_565)
                .error(R.mipmap.ic_launcher)
                .into(imageView);
    }

    /**
     * 网络图片显示
     */
    public static void Picasso_displayImage(Context context, String Url, ImageView imageView) {

        Picasso.with(context)
                .load(Url)
                .error(R.mipmap.ic_launcher)
                .into(imageView);
    }

    /**
     * 加载资源文件
     */
    public static void Picasso_displayImage(Context context, int resID,int width,int height, ImageView imageView) {

        Picasso.with(context)
                .load(resID)
                .resize(width,height)
                .error(R.mipmap.ic_launcher)
                .into(imageView);
    }

    /**
     * 加载资源文件
     */
    public static void Picasso_displayImage(Context context, int resID, ImageView imageView) {

        Picasso.with(context)
                .load(resID)
                .error(R.mipmap.ic_launcher)
                .into(imageView);
    }

    /**
     * 加载本地文件
     */
    public static void Picasso_displayImage_location(Context context, String locationPath, ImageView imageView) {

        Picasso.with(context)
                .load(new File(locationPath))
                .error(R.mipmap.ic_launcher)
                .into(imageView);
    }

    public Bitmap transform(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
                BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
//        mAccount.setHeadPic(bitmap);
        squaredBitmap.recycle();
        return bitmap;
    }
}